import { myGetRequest,myPostRequest } from "../../utils/myRequest.js"
//index.js
//获取应用实例
const app = getApp()

Component({
  data: {
    userInfo:{}
  },
  properties: {},
  lifetimes: {
    attached: async function() {
      var that = this;
      // 在组件实例进入页面节点树时执行
      // try {
        var userInfo = await that.getUserInfoById();
        if (userInfo) {
          this.setData({
              userInfo: userInfo
            })
        }
      // } catch (e) {
      //   // Do something when catch error
      //   console.log("页面初始化失败： "+e)
      // }
      // 
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
      
    },
  },
  methods: {
      // 去编辑页面
  gotoEditProfile() {
    wx.navigateTo({
      url: '/components/profile/editProfile/editProfile',
    })
  },
  gotoUserAppeal: function () {
    wx.navigateTo({
      url: '/pages/share/share',
    })
  },
  gotoUserDynamic: function () {
    wx.navigateTo({
      url: '/pages/index1/index1',
    })
  },
  gotoUserComment: function () {
    wx.navigateTo({
      url: '',
    })
  },
  getUserInfoById: async function() {
    var that = this
    var userInfo = wx.getStorageSync('userInfo');
    console.log(userInfo);
    var Id = userInfo.id;

    const res = await myGetRequest({url:'/user/getUserById?Id=' + Id});
    if( res.statusCode == 200 ){   
      if(res.data.code==0){
        var userInfo =res.data.data;
         return userInfo;
        }else{
        console.log(res.data.message);
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration:2500
        })
        return null;
      }
    }else{
      wx.showToast({
        title: '网络错误！',
        icon: 'error',
        duration:1500
      })
      return null;
    }
  }
  }
})